<?php

namespace app\model;

use think\Model;

class OrderGoods extends Model
{

    protected $connection = 'mysql';

    protected $pk = 'id';

    protected $name = 'order_goods';

    public function goods()
    {
        return $this->hasOne(Goods::class, 'id' , 'goods_id');
    }

    function getOrderGoods($order_id)
    {
        $data = $this::where(['order_id' => $order_id])->select()->toArray();

        foreach ($data as &$vo) {
            if (empty($vo["image"])) {
                $goods = Goods::where(['id' => $vo['goods_id']])->find();
                if (!empty($goods)) {
                    $goods = $goods->toArray();
                }

                if (!empty($goods)) {
                    $vo['pic'] = toimg($goods['image']);
                    $vo['image'] = toimg($goods['image']);
                }
            } else {
                $vo['image'] = toimg($vo['image']);
                $vo['pic'] = toimg($vo['image']);
            }
            $vo['label'] = $vo['sku'];
        }
        return $data;
    }
}
